package com.spzx.cart.controller;

import com.spzx.cart.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class CartController extends BaseController {

    @Autowired
    private CartService cartService;
    @GetMapping("/addToCart/{skuId}/{num}")
    public AjaxResult addToCart(@PathVariable("skuId") Long skuId, @PathVariable("num") Integer num){
        cartService.addToCart(skuId, num);
        return AjaxResult.success("修改商品数量成功");
    }

    @GetMapping("/cartList")
    public AjaxResult cartList(){
        List<CartInfo> cartList = cartService.getCartList();
        return AjaxResult.success(cartList);
    }

    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable("skuId") Long skuId){
        cartService.deleteCartBySkuId(skuId);
        return success();
    }

    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable("skuId") Long skuId, @PathVariable("isChecked") Integer isChecked){
        cartService.alertCheckCart(skuId, isChecked);
        return success();
    }

    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable("isChecked") Integer isChecked){
        cartService.alertAllCheckCart(isChecked);
        return success("修改成功");
    }

    @GetMapping("/clearCart")
    public AjaxResult clearCart(){
        cartService.clearCart();
        return success("清空购物车成功");
    }


    @InnerAuth
    @Operation(summary = "查询用户购物车列表中选中商品列表")
    @GetMapping("/getCartCheckedList/{userId}")
    public  R<List<CartInfo>> getCartCheckedList(@Parameter(name = "userId", description = "会员id", required = true) @PathVariable("userId") Long userId){
        List<CartInfo> cartCheckedList = cartService.getCartCheckedList(userId);
        return R.ok(cartCheckedList);
    }

    @Operation(summary="更新用户购物车列表价格")
    @InnerAuth
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable("userId") Long userId){
        return R.ok(cartService.updateCartPrice(userId));
    }

    @Operation(summary="删除用户购物车列表中选中商品列表")
    @InnerAuth
    @GetMapping("/deleteCartCheckedList/{userId}")
    public R<Boolean> deleteCartCheckedList(@PathVariable("userId") Long userId){
        return R.ok(cartService.deleteCartCheckedList(userId));
    }

}
